/* 
 * File:   State.cpp
 * Author: mvdr
 * 
 * Created on March 19, 2013, 3:30 PM
 */

#include "State.h"

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <sstream>

using namespace std;

State::State() {
    d_cards = new set<char>[3];
}

State::State(State const& other) {
    d_cards = new set<char>[3];
    for (int i = 0; i != 3; ++i)
        d_cards[i] = other.cards(i);
}

State::~State() {
    delete[] d_cards;
}

void State::setCards(int p, char c1, char c2) {
    d_cards[p].insert(c1);
    d_cards[p].insert(c2);
}

bool State::sameFor(State const &s, int p) const {
    int p1 = (p + 1) % 3;
    int p2 = (p + 2) % 3;
    
    return equal(d_cards[p1].begin(), d_cards[p1].end(), s.cards(p1).begin()) &
            equal(d_cards[p2].begin(), d_cards[p2].end(), s.cards(p2).begin());
}

ostream &operator<<(std::ostream& os, State const &st) {
    os << "{{";
    for (int i = 0; i != 3; ++i) {
        vector<int> tempVec (st.cards(i).begin(), st.cards(i).end());
        os << tempVec[0] << ',' << tempVec[1];
        if (i != 2)
            os << "},{";        
    }
    os << "}}";
    return os;
}

string State::toString() const {
    stringstream ss;
    ss << *this;
    return ss.str();
}
